package cqrtplm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.basic.log.TyplmLogger;
import com.hustcad.plm.basic.util.TyplmLoggerUtil;
import com.hustcad.plm.caddocapi.service.TyplmCADDocPermissionService;
import com.hustcad.plm.pdm.access.service.TyplmAccessJudgeService;
import com.hustcad.plm.pdm.common.model.vo.search.ResultEntityVO;
import com.hustcad.plm.pdm.common.service.TyplmCommonService;
import com.hustcad.plm.pdm.common.util.CommonUtil;
import com.hustcad.plm.pdm.doc.vo.DocDetails;
import com.hustcad.plm.pdm.file.model.constant.FileErrorCodeEnum;
import com.hustcad.plm.pdm.file.service.TyplmFileHandleService;
import com.hustcad.plm.pdm.filevault.model.vo.FileDataVO;
import com.hustcad.plm.pdm.middle.partbom.service.TyplmPdmPartService;
import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.hustcad.plm.pdm.partbom.service.TyplmPartBomService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartDescribeLinkService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartRelObjectService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.vo.PartBomTreeByFilterVO;
import com.hustcad.plm.pdm.partbom.vo.QueryDescribeObjVO;
import com.ty.basic.entity.CTyEntityBaseDO;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.entity.part.PartDO;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.session.TyAccountContext;
import com.ty.basic.utils.ArgumentUtils;
import com.ty.basic.utils.excel.converter.ListToExcel;
import com.ty.security.util.TyCommonUtils;
import cqrtplm.common.CommonService;
import cqrtplm.service.AssociatedDocService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service
@Slf4j
public class AssociatedDocImpl implements AssociatedDocService {
    private static final int MAX_RETRY = 150; //设置最大重试次数防止死循环
    @Resource
    private TyplmPdmPartService typlmPdmPartService;
    @Resource
    private TyplmPartDescribeLinkService typlmPartDescribeLinkService;
    @Resource
    private TyplmPartService typlmPartService;
    @Resource
    private CommonService commonService;
    @Resource
    private TyplmPartRelObjectService typlmPartRelObjectService;
    @Resource
    private TyplmPartBomService typlmPartBomService;
    @Resource
    private TyplmFileHandleService fileHandleService;
    @Resource
    private TyplmCommonService typlmCommonService;
    @Resource
    private TyplmAccessJudgeService typlmAccessJudgeService;
    @Resource
    private TyplmCADDocPermissionService typlmCADDocPermissionService;
    @Resource
    private TyplmLogger typlmLogger;

    private static CellStyle getHeadColumnStyle(Workbook workbook) {
        Font columnHeadFont = workbook.createFont();
        columnHeadFont.setFontName("宋体");
        columnHeadFont.setFontHeightInPoints((short) 10);
        columnHeadFont.setBold(true);
        CellStyle columnHeadStyle = getNormalColumnStyle(workbook);
        columnHeadStyle.setLocked(true);
        columnHeadStyle.setFont(columnHeadFont);
        columnHeadStyle.setBorderLeft(BorderStyle.THIN);
        columnHeadStyle.setBorderTop(BorderStyle.THIN);
        columnHeadStyle.setBorderRight(BorderStyle.THIN);
        columnHeadStyle.setBorderBottom(BorderStyle.THIN);
        columnHeadStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        return columnHeadStyle;
    }

    private static CellStyle getNormalColumnStyle(Workbook workbook) {
        Font columnFont = workbook.createFont();
        columnFont.setFontName("宋体");
        columnFont.setFontHeightInPoints((short) 10);
        CellStyle columnStyle = workbook.createCellStyle();
        columnStyle.setFont(columnFont);
        columnStyle.setAlignment(HorizontalAlignment.CENTER);
        columnStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        columnStyle.setWrapText(true);
        columnStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        columnStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        columnStyle.setBorderLeft(BorderStyle.THIN);
        columnStyle.setBorderTop(BorderStyle.THIN);
        columnStyle.setBorderRight(BorderStyle.THIN);
        columnStyle.setBorderBottom(BorderStyle.THIN);
        return columnStyle;
    }

    public List<Object> getPartLinkDocAndCadDoc(List<IdentifierEntity> identifierEntities) {
        List<Object> resultLst = new ArrayList();
        UserDO user = TyAccountContext.getUser();
        if (identifierEntities != null && !identifierEntities.isEmpty()) {
            for (IdentifierEntity identifierEntity : identifierEntities) {
                // 获取BOM结构树
                BigInteger oid = identifierEntity.getOid();
                String otype = identifierEntity.getOtype();
                PartDO partDO = this.typlmPartService.queryPartByOid(oid, otype);
                PartBomTreeByFilterVO bomTreeByFilterVO = this.commonService.queryChildParts(partDO.getPartnumber(), partDO.getOid().toString(), "Design", Boolean.FALSE, null, "N", Boolean.TRUE);
                List<PartBomTree> list = bomTreeByFilterVO.getPartBomTreeList();
                JSONObject jsonObject = null;
                if (list != null) {
                    List<BigInteger> oidList = new ArrayList<>();
                    for (PartBomTree partTree : list) {
                        oidList.add(new BigInteger(partTree.getPartoid()));
                        IdentifierEntity identifierEntity1 = new IdentifierEntity(new BigInteger(partTree.getPartoid()), partTree.getPartotype());
                        String partNumber = partTree.getObjectNumber();
                        List<IdentifierEntity> identifierEntitys1 = new ArrayList<>();
                        identifierEntitys1.add(identifierEntity1);
                        //查询关联的图纸
                        List<DocDetails> oidList1 = this.typlmPartRelObjectService.queryPartRelatedDocInfo(new BigInteger(partTree.getPartoid()));
                        if (oidList1 != null) {
                            for (DocDetails docDetails : oidList1) {
                                List<IdentifierEntity> entityList = new ArrayList<>();
                                IdentifierEntity identifier = new IdentifierEntity(new BigInteger(docDetails.getOid()), docDetails.getOtype());
                                entityList.add(identifier);
                                boolean hasRead = typlmAccessJudgeService.checkReadAccessByIdentifiers(entityList, user);
                                List<FileDataVO> filelist = docDetails.getPrimaryFileList();
                                if (filelist != null) {
                                    for (FileDataVO fileDataVO : filelist) {
                                        String docType = docDetails.getTypeName();
                                        String docNo = docDetails.getNumber();
                                        String docName = docDetails.getName();
                                        String fileName = fileDataVO.getFileName();
                                        String docVer = docDetails.getDisplayVersion();
                                        String docStatus = docDetails.getLifecycleStateName();
                                        String effectiveTime = "";
                                        if (StringUtils.isNotBlank(docStatus) && "已生效".equals(docStatus)) {
                                            effectiveTime = docDetails.getModifyTime();
                                        }
                                        String creator = docDetails.getCreatorFullName();
                                        String createTime = docDetails.getCreationTime();
                                        jsonObject = new JSONObject();
                                        jsonObject.put("docType", docType);
                                        jsonObject.put("docNo", docNo);
                                        jsonObject.put("partNo", partNumber);
                                        jsonObject.put("docName", docName);
                                        jsonObject.put("fileName", fileName);
                                        jsonObject.put("docVer", docVer);
                                        jsonObject.put("hasRead", hasRead);
                                        jsonObject.put("effectiveTime", effectiveTime);
                                        jsonObject.put("docStatus", docStatus);
                                        jsonObject.put("creator", creator);
                                        jsonObject.put("createTime", createTime);
                                        jsonObject.put("oid", docDetails.getOid());
                                        jsonObject.put("otype", docDetails.getOtype());
                                        resultLst.add(jsonObject);
                                    }
                                }
                            }
                        }
                        //查询关联的参考文档
                        List<DocDetails> oidList2 = this.typlmPdmPartService.getPartLinkReferenceDoc(partTree.getPartoid());
                        if (oidList2 != null) {
                            for (DocDetails docDetails : oidList2) {
                                String docOid = docDetails.getOid();
                                String oType = docDetails.getOtype();
                                List<IdentifierEntity> entityList = new ArrayList<>();
                                IdentifierEntity entity = new IdentifierEntity(new BigInteger(docOid), oType);
                                entityList.add(entity);
                                boolean hasRead = typlmAccessJudgeService.checkReadAccessByIdentifiers(entityList, user);
                                List<FileDataVO> fileListByEntity = this.fileHandleService.getFileListByEntity(entity);
                                if (fileListByEntity != null) {
                                    for (FileDataVO fileDataVO : fileListByEntity) {
                                        String rolekey = fileDataVO.getRolekey();
                                        if ("PRIMARY".equals(rolekey)) {
                                            String docType = docDetails.getTypeName();
                                            String docNo = docDetails.getNumber();
                                            String docName = docDetails.getName();
                                            String fileName = fileDataVO.getFileName();
                                            String docVer = docDetails.getDisplayVersion();
                                            String docStatus = docDetails.getLifecycleStateName();
                                            String effectiveTime = "";
                                            if (StringUtils.isNotBlank(docStatus) && "已生效".equals(docStatus)) {
                                                effectiveTime = docDetails.getModifyTime();
                                            }
                                            String creator = docDetails.getCreatorFullName();
                                            String createTime = docDetails.getCreationTime();
                                            jsonObject = new JSONObject();
                                            jsonObject.put("docType", docType);
                                            jsonObject.put("docNo", docNo);
                                            jsonObject.put("partNo", partNumber);
                                            jsonObject.put("docName", docName);
                                            jsonObject.put("fileName", fileName);
                                            jsonObject.put("docVer", docVer);
                                            jsonObject.put("hasRead", hasRead);
                                            jsonObject.put("effectiveTime", effectiveTime);
                                            jsonObject.put("docStatus", docStatus);
                                            jsonObject.put("creator", creator);
                                            jsonObject.put("createTime", createTime);
                                            jsonObject.put("oid", docOid);
                                            jsonObject.put("otype", oType);
                                            resultLst.add(jsonObject);

                                        }
                                    }
                                }
                            }
                        }
                        //查询关联的描述文档
                        List<QueryDescribeObjVO> describeObjVOList = this.typlmPartDescribeLinkService.queryPartDescribeDoc(identifierEntitys1);
                        if (describeObjVOList != null) {
                            for (QueryDescribeObjVO queryDescribeObjVO : describeObjVOList) {
                                List<ResultEntityVO> describeObjList = queryDescribeObjVO.getDescribeObjList();
                                if (describeObjList != null) {
                                    for (ResultEntityVO resultEntityVO : describeObjList) {
                                        BigInteger docOid = resultEntityVO.getOid();
                                        String oType = resultEntityVO.getOtype();
                                        IdentifierEntity entity = new IdentifierEntity(docOid, oType);
                                        List<IdentifierEntity> entityList = new ArrayList<>();
                                        entityList.add(entity);
                                        boolean hasRead = typlmAccessJudgeService.checkReadAccessByIdentifiers(entityList, user);
                                        List<FileDataVO> fileListByEntity = this.fileHandleService.getFileListByEntity(entity);
                                        if (fileListByEntity != null) {
                                            for (FileDataVO fileDataVO : fileListByEntity) {
                                                String rolekey = fileDataVO.getRolekey();
                                                if ("PRIMARY".equals(rolekey)) {
                                                    String docType = resultEntityVO.getTypeDefName();
                                                    String docNo = resultEntityVO.getObjectNumber();
                                                    String docName = resultEntityVO.getName();
                                                    String fileName = fileDataVO.getFileName();
                                                    String docVer = resultEntityVO.getMajorVersion() + "." + resultEntityVO.getMinorVersion();
                                                    String docStatus = resultEntityVO.getLifecyclestagekeyName();
                                                    String effectiveTime = "";
                                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                                    if (StringUtils.isNotBlank(docStatus) && "已生效".equals(docStatus)) {
                                                        effectiveTime = sdf.format(resultEntityVO.getModifystamp());
                                                    }
                                                    String creator = resultEntityVO.getCreatorFullName();
                                                    String createTime = sdf.format(resultEntityVO.getCreatestamp());
                                                    jsonObject = new JSONObject();
                                                    jsonObject.put("docType", docType);
                                                    jsonObject.put("docNo", docNo);
                                                    jsonObject.put("partNo", partNumber);
                                                    jsonObject.put("docName", docName);
                                                    jsonObject.put("fileName", fileName);
                                                    jsonObject.put("docVer", docVer);
                                                    jsonObject.put("hasRead", hasRead);
                                                    jsonObject.put("effectiveTime", effectiveTime);
                                                    jsonObject.put("docStatus", docStatus);
                                                    jsonObject.put("creator", creator);
                                                    jsonObject.put("createTime", createTime);
                                                    jsonObject.put("oid", docOid.toString());
                                                    jsonObject.put("otype", oType);
                                                    resultLst.add(jsonObject);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return resultLst;
    }

    /**
     * 导出关联图文档清单
     *
     * @param object
     * @return
     */
    @Override
    public void exportDocList(Object object, HttpServletResponse response) throws IOException {
        if (object != null) {
            LinkedHashMap map = (LinkedHashMap) object;
            try {
                if (map.containsKey("columnJson") && map.containsKey("docList")) {
                    TyplmLoggerUtil.debug(log, () -> "获取导入表头的显示列");
                    LinkedHashMap columnJson = (LinkedHashMap) map.get("columnJson");
                    List list2 = (ArrayList) map.get("docList");
                    Workbook workBook = new HSSFWorkbook();
                    CellStyle style = getHeadColumnStyle(workBook);//单元格样式
                    CellStyle cellStyle = getNormalColumnStyle(workBook);
                    Sheet sheet = null;
                    //创建sheet页
                    sheet = workBook.createSheet();
                    //sheet页名称
                    workBook.setSheetName(0, "图文档清单");
                    //创建header页
                    Header header1 = sheet.getHeader();
                    //设置标题居中
                    header1.setCenter("图文档清单");
                    Row row = sheet.createRow(0);
                    Cell cell = row.createCell(0);
                    cell.setCellStyle(style);
                    cell.setCellValue("序号");
                    sheet.setColumnWidth(0, 2000);
                    int index = 0;
                    for (Object key : columnJson.keySet()) {
                        index++;
                        Cell cell2 = row.createCell(index);
                        cell2.setCellStyle(style);
                        cell2.setCellValue(columnJson.get(key).toString());
                        sheet.setColumnWidth(index, 5000);
                    }
                    for (int j = 0; j < list2.size(); j++) {
                        index = 0;
                        LinkedHashMap jsonObject = (LinkedHashMap) list2.get(j);
                        Row row1 = sheet.createRow(j + 1);
                        Cell cell10 = row1.createCell(index);
                        cell10.setCellStyle(cellStyle);
                        cell10.setCellValue(j + 1);
                        for (Object key : jsonObject.keySet()) {
                            index++;
                            Cell cell11 = row1.createCell(index);
                            cell11.setCellStyle(cellStyle);
                            cell11.setCellValue(jsonObject.get(key).toString());
                        }
                    }
                    ListToExcel.downloadExcelByBrowser(response, workBook, "图文档清单.xls");
                }
            } catch (Throwable $ex) {
                throw $ex;
            }
        }
    }

    /**
     * 图文档打包下载
     *
     * @param identifierEntities
     * @return
     */
    @Override
    public ResponseResult<byte[]> downloadFilesAsZip(List<IdentifierEntity> identifierEntities) {
        TyAccountContext.setBizCommonHolderMapValue("ignoreUserDisabled", "Y");
        UserDO user = TyAccountContext.getUser();
        try {
            List<BigInteger> fileIdList = new ArrayList<>();
            for (IdentifierEntity entity : identifierEntities) {
                List<IdentifierEntity> entityList=new ArrayList<>();
                entityList.add(entity);
                boolean hasRead = typlmAccessJudgeService.checkReadAccessByIdentifiers(entityList, user);
                if (hasRead) {
                    TyCommonUtils.requireNonNull(entity, "entity");
                    CommonUtil.checkIdentifierNotNull(entity);
                    List<FileDataVO> fileListByEntity = this.fileHandleService.getFileListByEntity(entity);
                    if (fileListByEntity != null) {
                        for (FileDataVO fileDataVO : fileListByEntity) {
                            fileIdList.add(new BigInteger(fileDataVO.getFileID()));
                        }
                    }
                }
            }
            ArgumentUtils.checkArgumentEmpty(fileIdList, FileErrorCodeEnum.FILE_ID.getLocaleMessage());
            // 日志记录前置，确保即使下载失败也能记录
            fileIdList.forEach(oid -> {
                this.typlmLogger.info(new CTyEntityBaseDO(oid, "ty.inteplm.fileVault.CTyContentData"), LogEventEnum.DOWNLOAD);
            });
            Map<String, byte[]> fileMap;
            try {
                fileMap = this.fileHandleService.downloadFile(fileIdList);
            } catch (Exception e) {
                throw new RuntimeException("文件下载失败", e);
            }
            // 创建ZIP文件
            byte[] zipData = createZipFile(fileMap);
            return ResponseResult.success(zipData);
        } finally {
            TyAccountContext.removeBizCommonHolderMapValue("ignoreUserDisabled");
        }
    }

    // 创建ZIP文件的方法
    private byte[] createZipFile(Map<String, byte[]> fileMap) {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos)) {

            for (Map.Entry<String, byte[]> entry : fileMap.entrySet()) {
                ZipEntry zipEntry = new ZipEntry(entry.getKey());
                zos.putNextEntry(zipEntry);
                zos.write(entry.getValue());
                zos.closeEntry();
            }

            zos.finish();
            return baos.toByteArray();
        } catch (Exception e) {

            throw new RuntimeException("创建ZIP文件失败", e);
        }
    }

    /**
     * 查询图文档清单
     *
     * @param map
     * @return
     */
    @Override
    public List<Object> queryDocListByKeyWord(LinkedHashMap map) {
        List<Object> resultLst = new ArrayList();
        if (map != null) {
            String oid = map.get("oid").toString();
            String otype = map.get("otype").toString();
            String keyword = map.get("keyword").toString();
            PartDO partDO = this.typlmPartService.queryPartByOid(new BigInteger(oid), otype);
            PartBomTreeByFilterVO bomTreeByFilterVO = this.commonService.queryChildParts(partDO.getPartnumber(), partDO.getOid().toString(), "Design", Boolean.FALSE, null, "N", Boolean.TRUE);
            List<PartBomTree> list = bomTreeByFilterVO.getPartBomTreeList();
            JSONObject jsonObject = null;
            if (list != null) {
                List<BigInteger> oidList = new ArrayList<>();
                List<IdentifierEntity> identifierEntitys1 = new ArrayList<>();
                for (PartBomTree partTree : list) {
                    oidList.add(new BigInteger(partTree.getPartoid()));
                    IdentifierEntity identifierEntity1 = new IdentifierEntity(new BigInteger(partTree.getPartoid()), partTree.getPartotype());
                    PartDO partDO2 = this.typlmPartService.queryPartByOid(new BigInteger(partTree.getPartoid()), partTree.getPartotype());
                    identifierEntitys1.add(identifierEntity1);
                    List<DocDetails> oidList1 = this.typlmPartRelObjectService.queryPartRelatedDocInfo(new BigInteger(partTree.getPartoid()));
                    if (oidList1 != null) {
                        for (DocDetails docDetails : oidList1) {
                            List<FileDataVO> filelist = docDetails.getPrimaryFileList();
                            if (filelist != null) {
                                for (FileDataVO fileDataVO : filelist) {
                                    String docType = docDetails.getTypeName();
                                    String docNo = docDetails.getNumber();
                                    String docName = docDetails.getName();
                                    String fileName = fileDataVO.getFileName();
                                    String docVer = docDetails.getDisplayVersion();
                                    String docStatus = docDetails.getLifecycleStateName();
                                    String effectiveTime = "";
                                    if (StringUtils.isNotBlank(docStatus) && "已生效".equals(docStatus)) {
                                        effectiveTime = docDetails.getModifyTime();
                                    }
                                    String creator = docDetails.getCreatorFullName();
                                    String createTime = docDetails.getCreationTime();
                                    if (StringUtils.isNotBlank(keyword)) {
                                        if (!docType.contains(keyword) && !docNo.contains(keyword) && !docName.contains(keyword) && !fileName.contains(keyword) && !docVer.contains(keyword) && !docStatus.contains(keyword) && !creator.contains(keyword)) {
                                            continue;
                                        }
                                    }
                                    jsonObject = new JSONObject();
                                    jsonObject.put("docType", docType);
                                    jsonObject.put("docNo", docNo);
                                    jsonObject.put("docName", docName);
                                    jsonObject.put("fileName", fileName);
                                    jsonObject.put("docVer", docVer);
                                    jsonObject.put("effectiveTime", effectiveTime);
                                    jsonObject.put("docStatus", docStatus);
                                    jsonObject.put("creator", creator);
                                    jsonObject.put("createTime", createTime);
                                    jsonObject.put("oid", docDetails.getOid());
                                    jsonObject.put("otype", docDetails.getOtype());
                                    resultLst.add(jsonObject);
                                }
                            }
                        }
                    }
                    List<DocDetails> oidList2 = this.typlmPdmPartService.getPartLinkReferenceDoc(partTree.getPartoid());
                    if (oidList2 != null) {
                        for (DocDetails docDetails : oidList2) {
                            String docOid = docDetails.getOid();
                            String oType = docDetails.getOtype();
                            IdentifierEntity entity = new IdentifierEntity(new BigInteger(docOid), oType);
                            List<FileDataVO> fileListByEntity = this.fileHandleService.getFileListByEntity(entity);
                            if (fileListByEntity != null) {
                                for (FileDataVO fileDataVO : fileListByEntity) {
                                    String rolekey = fileDataVO.getRolekey();
                                    if ("PRIMARY".equals(rolekey)) {
                                        String docType = docDetails.getTypeName();
                                        String docNo = docDetails.getNumber();
                                        String docName = docDetails.getName();
                                        String fileName = fileDataVO.getFileName();
                                        String docVer = docDetails.getDisplayVersion();
                                        String docStatus = docDetails.getLifecycleStateName();
                                        String effectiveTime = "";
                                        if (StringUtils.isNotBlank(docStatus) && "已生效".equals(docStatus)) {
                                            effectiveTime = docDetails.getModifyTime();
                                        }
                                        String creator = docDetails.getCreatorFullName();
                                        String createTime = docDetails.getCreationTime();
                                        if (StringUtils.isNotBlank(keyword)) {
                                            if (!docType.contains(keyword) && !docNo.contains(keyword) && !docName.contains(keyword) && !fileName.contains(keyword) && !docVer.contains(keyword) && !docStatus.contains(keyword) && !creator.contains(keyword)) {
                                                continue;
                                            }
                                        }
                                        jsonObject = new JSONObject();
                                        jsonObject.put("docType", docType);
                                        jsonObject.put("docNo", docNo);
                                        jsonObject.put("docName", docName);
                                        jsonObject.put("fileName", fileName);
                                        jsonObject.put("docVer", docVer);
                                        jsonObject.put("effectiveTime", effectiveTime);
                                        jsonObject.put("docStatus", docStatus);
                                        jsonObject.put("creator", creator);
                                        jsonObject.put("createTime", createTime);
                                        jsonObject.put("oid", docOid);
                                        jsonObject.put("otype", oType);
                                        resultLst.add(jsonObject);
                                    }
                                }
                            }
                        }
                    }
                }
                List<QueryDescribeObjVO> describeObjVOList = this.typlmPartDescribeLinkService.queryPartDescribeDoc(identifierEntitys1);
                if (describeObjVOList != null) {
                    for (QueryDescribeObjVO queryDescribeObjVO : describeObjVOList) {
                        List<ResultEntityVO> describeObjList = queryDescribeObjVO.getDescribeObjList();
                        if (describeObjList != null) {
                            for (ResultEntityVO resultEntityVO : describeObjList) {
                                BigInteger docOid = resultEntityVO.getOid();
                                String oType = resultEntityVO.getOtype();
                                IdentifierEntity entity = new IdentifierEntity(docOid, oType);
                                List<FileDataVO> fileListByEntity = this.fileHandleService.getFileListByEntity(entity);
                                if (fileListByEntity != null) {
                                    for (FileDataVO fileDataVO : fileListByEntity) {
                                        String rolekey = fileDataVO.getRolekey();
                                        if ("PRIMARY".equals(rolekey)) {
                                            String docType = resultEntityVO.getTypeDefName();
                                            String docNo = resultEntityVO.getObjectNumber();
                                            String docName = resultEntityVO.getName();
                                            String fileName = fileDataVO.getFileName();
                                            String docVer = resultEntityVO.getMajorVersion() + "." + resultEntityVO.getMinorVersion();
                                            String docStatus = resultEntityVO.getLifecyclestagekeyName();
                                            String effectiveTime = "";
                                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                            if (StringUtils.isNotBlank(docStatus) && "已生效".equals(docStatus)) {
                                                effectiveTime = sdf.format(resultEntityVO.getModifystamp());
                                            }
                                            String creator = resultEntityVO.getCreatorFullName();
                                            String createTime = sdf.format(resultEntityVO.getCreatestamp());
                                            if (StringUtils.isNotBlank(keyword)) {
                                                if (!docType.contains(keyword) && !docNo.contains(keyword) && !docName.contains(keyword) && !fileName.contains(keyword) && !docVer.contains(keyword) && !docStatus.contains(keyword) && !creator.contains(keyword)) {
                                                    continue;
                                                }
                                            }
                                            jsonObject = new JSONObject();
                                            jsonObject.put("docType", docType);
                                            jsonObject.put("docNo", docNo);
                                            jsonObject.put("docName", docName);
                                            jsonObject.put("fileName", fileName);
                                            jsonObject.put("docVer", docVer);
                                            jsonObject.put("effectiveTime", effectiveTime);
                                            jsonObject.put("docStatus", docStatus);
                                            jsonObject.put("creator", creator);
                                            jsonObject.put("createTime", createTime);
                                            jsonObject.put("oid", docOid.toString());
                                            jsonObject.put("otype", oType);
                                            resultLst.add(jsonObject);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return resultLst;
    }

    @Override
    public List<DocDetails> getPartLinkCompleteList(List<IdentifierEntity> identifierEntities) {
        return Collections.emptyList();
    }
}
